package com.neuedu.api.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/2411:25
 */
public class MyDate {

    public static void main(String[] args) {


        //对象的创建
        Date  now = new Date();  //代表当前时间


        Date  date1 = new Date(2024-1900,10-1,24,11,29,15);

        System.out.println(now);
        System.out.println(date1);





        //类的常用方法
        //整形的形式表示的  数字 1970 1 1   1729740555000
        System.out.println("date1.getTime() = " + date1.getTime());
        System.out.println("System.currentTimeMillis() = " + System.currentTimeMillis());

        // 获取日期中的某一个字段
        int year = now.getYear() + 1900 ;
        int month = now.getMonth() + 1 ;
        int date = now.getDate();   // 当前月 第几天
        int day = now.getDay(); //星期几
        int hours = now.getHours(); //
        int min = now.getMinutes(); //
        int seconds = now.getSeconds(); //


        System.out.println(year+"-"+month+"-"+date+" "+hours+":"+min+":"+seconds);


        //格式化        yyyy
        /***
         * Letter  Date or Time Component  Presentation  Examples
         * G  Era designator  Text  AD
         * y  Year  Year  1996; 96
         * Y  Week year  Year  2009; 09
         * M  Month in year (context sensitive)  Month  July; Jul; 07
         * L  Month in year (standalone form)  Month  July; Jul; 07
         * w  Week in year  Number  27
         * W  Week in month  Number  2
         * D  Day in year  Number  189
         * d  Day in month  Number  10
         * F  Day of week in month  Number  2
         * E  Day name in week  Text  Tuesday; Tue
         * u  Day number of week (1 = Monday, ..., 7 = Sunday)  Number  1
         * a  Am/pm marker  Text  PM
         * H  Hour in day (0-23)  Number  0
         * k  Hour in day (1-24)  Number  24
         * K  Hour in am/pm (0-11)  Number  0
         * h  Hour in am/pm (1-12)  Number  12
         * m  Minute in hour  Number  30
         * s  Second in minute  Number  55
         * S  Millisecond  Number  978
         * z  Time zone  General time zone  Pacific Standard Time; PST; GMT-08:00
         * Z  Time zone  RFC 822 time zone  -0800
         * X  Time zone  ISO 8601 time zone  -08; -0800; -08:00
         */
        //使用
        String pattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);


        System.out.println("sdf.format(now) = " + sdf.format(now));

        Date d1 = null;
        Date d2 = null;
        try {
            d1  =sdf.parse("2024-10-15 11:53:01");
            d2  =sdf.parse("2024-10-16 11:53:01");
        } catch (ParseException e) {
            //java.text.ParseException: Unparseable date: "-10-15 11:53:01"
           e.printStackTrace();
        }


        System.out.println("d1 = " + d1);
        System.out.println("d2 = " + d2);
        // 日期的比较
        System.out.println("d1.before(d2) = " + d1.before(d2));
        System.out.println("d2.before(d1) = " + d2.before(d1));
        System.out.println("d1.before(d1) = " + d1.before(d1));

        System.out.println("d1.after(d2) = " + d1.after(d2));  //false
        System.out.println("d2.after(d1) = " + d2.after(d1));  //true
        System.out.println("d1.after(d1) = " + d1.after(d1));   //false

        System.out.println("d1.compareTo(d2) = " + d1.compareTo(d2));  //
        System.out.println("d2.compareTo(d1) = " + d2.compareTo(d1));  //
        System.out.println("d1.compareTo(d1) = " + d1.compareTo(d1));   //f
    }
}
